package com.reggie.controller.user;

import com.reggie.entity.Dish;
import com.reggie.mapper.DishMapper;
import com.reggie.result.R;
import com.reggie.service.DishService;
import com.reggie.vo.DishVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@Slf4j
@RestController("userDishController")
@RequestMapping("/user/dish")
@Api(tags = "C端-菜品浏览接口")
public class DishController {

    @Autowired
    private DishService dishService;
    @Autowired
    private RedisTemplate redisTemplate;

    @GetMapping("/list")
    @ApiOperation("根据分类id查询菜品")
    public R<List<DishVO>> findCatrgoryId(Long categoryId){
        log.info("根据分类id查询菜品的参数：{}",categoryId);
        String Key = "dish_" + categoryId;

        //先查询redis中是否有数据，
        List<DishVO> dishVoList = (List<DishVO>) redisTemplate.opsForValue().get(Key);
        if (dishVoList != null){
            // 如果有就从缓存数据库中取出
            return R.success(dishVoList);
        }
        //查询数据库
        dishVoList = dishService.findCategoryId(categoryId);
        //如果没有就查询数据库，然后在存到redis中
        redisTemplate.opsForValue().set(Key,dishVoList);
        return R.success(dishVoList);
    }
}
